package com.cpr.core.rabbitmq.client.pubsub;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.nio.charset.StandardCharsets;

/**
 * @author Chen Panrui
 * created at 2022/3/15 14:36
 *
 * 广播模式
 * rabbitmqctl list_exchanges --formatter pretty_table
 * rabbitmqctl list_bindings --formatter pretty_table
 *
 */
public class Producer {
    public static void main(String[] args) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        // 指定协议 用户名+密码+host+端口号+虚拟主机
        factory.setUri("amqp://admin:123456@106.13.89.7:5672/%2f");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        // 不需要关系 exchange后续的队列
        channel.exchangeDeclare("ex.myFan", BuiltinExchangeType.FANOUT, true, false, null);
        // 不需要管 routeKey
        for (int i = 0; i < 20; i++) {
            channel.basicPublish("ex.myFan", "", null, ("你好"+i).getBytes(StandardCharsets.UTF_8));
        }

        channel.close();
        connection.close();
    }
}
